package com.tom.rxlearn;

import android.os.Bundle;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.tom.rxlearn.utils.DMLog;

import java.util.concurrent.atomic.AtomicReferenceArray;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        findViewById(R.id.test_rx).setOnClickListener(v -> {
            doRxJavaTest();
        });
        findViewById(R.id.AtomicReferenceArray).setOnClickListener(v -> {
            AtomicReferenceArray();
        });
    }

    private void AtomicReferenceArray() {
        AtomicReferenceArray reference = new AtomicReferenceArray(5);
        reference.compareAndSet(0, "dsklsadlsa", "123456789");

        AtomicReferenceArray reference1 = new AtomicReferenceArray(5);
        Object obj = reference1.get(0);
        Toast.makeText(this, obj == null ? "null" : obj.toString(), Toast.LENGTH_SHORT).show();
    }

    private void doRxJavaTestTwo() {
        Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> emitter) throws Exception {

            }
        })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe();
    }

    private void doRxJavaTest() {

        Observable<Integer> just = Observable.just(1);
        DMLog.e(this.getClass().getSimpleName(), just.getClass().getName());

        // ObservableSubscribeOn 与 ObservableObserveOn 哪个放在前面，哪个的subscribeActual方法就后执行
        //ObservableSubscribeOn subscribeActual
        Observable<Integer> subscribeOn = just.subscribeOn(Schedulers.io());
        DMLog.e(this.getClass().getSimpleName(), subscribeOn.getClass().getName());

        //ObservableObserveOn subscribeActual
        Observable<Integer> observeOn = subscribeOn.observeOn(AndroidSchedulers.mainThread());
        DMLog.e(this.getClass().getSimpleName(), observeOn.getClass().getName());

        observeOn.subscribe(new Observer<Integer>() {
            @Override
            public void onSubscribe(Disposable d) {
                DMLog.e(this.getClass().getSimpleName(), "onSubscribe " + Thread.currentThread().getName());
            }

            @Override
            public void onNext(Integer integer) {
                DMLog.e(this.getClass().getSimpleName(), "onNext " + Thread.currentThread().getName());
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {
                DMLog.e(this.getClass().getSimpleName(), "onComplete " + Thread.currentThread().getName());
            }
        });
    }
}
